class Crawler::SourceMonitorJob < Crawler::Job
  
  def initialize(crawler)
    super(crawler)
    
    # at first, set deadline to immediate b/c sources are needed
    @deadline = Time.now
  end
  
  def run
    # updates sources from database, detecting any changes made by other processes.
    if @crawler.sources.nil?
      log_info("Loading sources.")
      @crawler.sources = Source.all
      set_crawler_refs
    elsif Source.check_for_flag_file?
      log_info("Found source updates, reloading.")

      # get all sources that need reloading, all new ones, and all that have been deleted
      @crawler.sources.match(Source.all) do |older, newer|
        # if new, add to array
        if older.nil?
          @crawler.sources << newer
        elsif newer.nil?
          @crawler.sources.delete(older)
        elsif newer.reset_needed?
          older.reset!
        end
      end

      set_crawler_refs
      
      enabled_count = @crawler.sources.reject{|s| !s.enabled?}.size
      log_info("Sources updated. Now using #{enabled_count} enabled sources.")
      
      Source.remove_flag_file
    else
      log_info("No source update needed.", :verbosity => 2)
    end
    
    @deadline = Time.now + 5.seconds
  end

  private
    def set_crawler_refs
      @crawler.sources.each{|s| s.crawler = @crawler}
    end
end